﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Glodon.Gdmp.DB;
using Glodon.Gdmp.UI;

namespace Glodon.Gdmp.SDK.Samples.ElementColorOverride
{
    /// <summary>
    /// 此示例展示了使用图形样式对图元进行着色以方便模型校审。
    /// </summary>
    public class ElementColorOverrideCmd : IExternalCommand
    {
        private Document _document;
        public Result Execute(UIDocument uiDocument)
        {
            try
            {
                UIApplication app = UIApplication.Get(); 
                string location = Assembly.GetExecutingAssembly().Location;
                string sampleFilePath = Path.Combine($"{ Path.GetDirectoryName(location)}", "ElementColorOverrideSample.glab");
                //
                // 文档从未打开时运行，或者运行后关闭文档重新运行，需要打开文档。
                if (_document == null || !_document.IsValidObject())
                {
                    // 保存文档对象，下次运行时，如果文档还未关闭，可以复用。
                    var newUIDocument = app.OpenAndActiveDocument(sampleFilePath);
                    _document = newUIDocument.Document;
                }

                using (var transaction = new Transaction(_document, "图元图形样式重载"))
                {
                    transaction.Start();
                    // 创建图形样式。
                    GraphicsStyle graphicsStyle = GraphicsStyle.Create(_document);
                    GraphicsStyleData graphicsStyleData = GraphicsStyleData.Create();
                    // 指定样式颜色，如中国红。
                    Color color = new Color(255, 0, 0);
                    graphicsStyleData.ProjectionLineColor = color;
                    graphicsStyleData.SectionLineColor = color;
                    graphicsStyleData.HiddenLineColor = color;
                    graphicsStyleData.Color = color;
                    graphicsStyleData.SectionFaceColor = color;
                    graphicsStyleData.ProjectionFaceHatchColor = color;
                    graphicsStyleData.SectionFaceHatchColor = color;
                    graphicsStyle.SetGraphicsStyleData(graphicsStyleData);
                    // 获取图形样式管理器。
                    GraphicsStyleManager graphicsStyleManager = GraphicsStyleManager.Get(_document);
                    // 罗列需要变更颜色的图元Id。
                    List<ElementId> ids = new List<ElementId>()
                    {
                        new ElementId(18014123631575914),
                        new ElementId(18014123631575917),
                    };
                    // 使用设置的颜色覆盖图元原始颜色。
                    graphicsStyleManager.OverrideGraphicsStyle(ids, graphicsStyle.Id);
                    transaction.Commit();
                }
                uiDocument.CurrentUIView.Refresh();

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                return Result.Failed;
            }
        }
    }
}
